package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.article.ArticleHistoryHandler;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.vo.SubjectRecommend;

public class SimpleRecommendItem extends VerticalPanel {

	public SimpleRecommendItem(final SubjectRecommend recommend) {
		HorizontalPanel innerPanel = new HorizontalPanel();
		addStyleName("recommend-simple");
		Image img = new Image(recommend.getUser().getPortrait());
		img.addStyleName("recommend-simple-image");
		innerPanel.add(img);
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.addStyleName("recommend-simple-content");
		HorizontalPanel titlePanel = new HorizontalPanel();
		final HTML title = new HTML("<a>" + recommend.getTitle() + "</a>");
		title.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(ArticleHistoryHandler.PREFIX_RECOMMEND+recommend.getId());
			}
		});
		title.addStyleName("recommend-simple-title");
		titlePanel.add(title);
		{
			final DecoratedPopupPanel popup = new DecoratedPopupPanel();
			popup.addStyleName("recommend-simple-popup");
			SimplePanel sPanel = new SimplePanel();
			sPanel.setWidget(new HTML(recommend.getContent()));
			popup.setWidget(sPanel);
			title.addMouseOverHandler(new MouseOverHandler() {
				@Override
				public void onMouseOver(MouseOverEvent event) {
					popup.setPopupPosition(title.getAbsoluteLeft()+title.getOffsetWidth()+5, title.getAbsoluteTop()-5);
					popup.show();
				}
			});
			title.addMouseOutHandler(new MouseOutHandler() {
				@Override
				public void onMouseOut(MouseOutEvent event) {
					popup.hide();
				}
			});
		}
		vPanel.add(titlePanel);
		HorizontalPanel panel = new HorizontalPanel();
		Label label = new Label("作者:");
		label.addStyleName("recommend-simple-label");
		HTML name = new HTML("<a>" + recommend.getUser().getUsername() + "</a>");
		name.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(PHPHistoryHandler.PREFIX_USERHOME+recommend.getUser().getId());
			}
		});
		name.addStyleName("recommend-simple-name");
		panel.add(label);
		panel.add(name);
		if (recommend.getDate() != null) {
			label = new Label("" + recommend.getDate());
			label.addStyleName("recommend-simple-date");
			panel.add(label);
		}
		vPanel.add(panel);
		panel = new HorizontalPanel();
		label = new Label("评论:");
		label.addStyleName("recommend-simple-label");
		panel.add(label);
		HTML entity = new HTML("<a>" + recommend.getEntityName() + "</a>");
		entity.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				System.out.println(recommend.getEntityId());
				History.newItem(ArticleHistoryHandler.PREFIX_ENTITY+recommend.getEntityId());
			}
		});
		entity.addStyleName("recommend-simple-entity");
		panel.add(entity);
		vPanel.add(panel);
		HorizontalPanel scorePanel = new HorizontalPanel();
		scorePanel.addStyleName("recommend-simple-score");
		label = new Label("评分:");
		label.addStyleName("score-text");
		scorePanel.add(label);
		Label score = new Label(recommend.getScore() + "分");
		score.addStyleName("star" + recommend.getScore().intValue());
		scorePanel.add(score);
		score.setTitle(recommend.getScore() + "分");
		vPanel.add(scorePanel);
		innerPanel.add(vPanel);
		add(innerPanel);

		title.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(ArticleHistoryHandler.PREFIX_RECOMMEND
						+ recommend.getId());
			}
		});

	}
}
